package com.wang.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.mall.bean.SmsSeckillSession;
import com.wang.mall.model.R;
import com.wang.mall.service.SkillService;
import com.wang.mall.service.SmsSeckillSessionService;
import com.wang.mall.to.SeckillSkuRedisTo;
import com.wang.mall.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/skill")
public class SkillController {

    @Autowired
    private SmsSeckillSessionService smsSeckillSessionService;

    @Autowired
    private SkillService skillService;

    /**
     * 发送秒杀信息,秒杀数据
     *
     * @param productId
     * @param sessionId
     * @param code
     * @param count
     * @return
     */
    @GetMapping("/panicBuying")
    public R panicBuying(Long productId, Integer sessionId, String code, Integer count) {
        String orderSn = skillService.kill(productId, sessionId, code, count);
        return R.ok(orderSn);
    }

    /**
     * 获取当前商品的秒杀信息
     *
     * @return
     */
    @GetMapping("/productSkillInfo/{productId}/{session}")
    public R productSkillInfo(@PathVariable Long productId, @PathVariable Integer session) {
        SeckillSkuRedisTo to = skillService.productSkillInfo(productId, session);
        return R.ok(to);
    }

    /**
     * 返回当前时间可以参与的秒杀商品信息
     *
     * @return
     */
    @GetMapping("/getCurrentSkillProduct")
    public R getCurrentSkillProduct() {
        List<SeckillSkuRedisTo> skuRedisTos = skillService.getCurrentSkillProduct();
        return R.ok(skuRedisTos);
    }

    /**
     * 分页条件查询
     *
     * @param page
     * @param startTime
     * @param endTime
     * @param createTime
     * @return
     */
    @GetMapping
    public R queryPage(Page<SmsSeckillSession> page, String startTime, String endTime, String createTime) {
        PageUtils page1 = smsSeckillSessionService.queryPage(page, startTime, endTime, createTime);
        return R.ok(page1);
    }


    @PutMapping("/updateStatus")
    public R updateStatus(@RequestBody SmsSeckillSession smsSeckillSession) {
        boolean b = smsSeckillSessionService.updateById(smsSeckillSession);
        return b ? R.ok() : R.fail("修改失败");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("coupon:seckillsession:info")
    public R info(@PathVariable("id") Long id) {
        SmsSeckillSession seckillSession = smsSeckillSessionService.getById(id);

        return R.ok(seckillSession);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("coupon:seckillsession:save")
    public R save(@RequestBody SmsSeckillSession seckillSession) {

        // TODO 查询该秒杀时间段内有没有其他的秒杀活动
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date startData = seckillSession.getStartTime();
        Date endData = seckillSession.getEndTime();

        String startTime = format.format(startData);
        String endTime = format.format(endData);


        seckillSession.setCreateTime(new Date());
        smsSeckillSessionService.save(seckillSession);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("coupon:seckillsession:update")
    public R update(@RequestBody SmsSeckillSession seckillSession) {
        smsSeckillSessionService.updateById(seckillSession);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("coupon:seckillsession:delete")
    public R delete(@RequestBody Long[] ids) {
        smsSeckillSessionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
